package com.lanyou.esb.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.esb.datadictionary.IfParameterType;

/**
 * 接口参数
 * 
 * @author Davey.wu
 */
@Entity
@EntityListeners({ AuditingEntityListener.class })
@Table(name = "T_ESB_BU_IF_PARAMETER")
public class IfParameter {
	/** 接口参数表ID **/
	private String id;

	/** 接口 **/
	private Http http;

	/** 参数类型 **/
	private String type;

	/** 接口参数数据类型 **/
	private String dataType;

	/** 接口参数名称 **/
	private String name;

	/** 接口参数是否必须 **/
	private String isNeed;

	/** 参数默认值 **/
	private String defaultValue;

	/** 接口参数描述 **/
	private String desc;

	/** 接口返回参数位置 **/
	private String pos;

	/** 是否可用 **/
	private String isEnable;

	/** 创建人 **/
	private String creator;

	/** 创建时间 **/
	private Date createTime;

	/** 最后更新人员 **/
	private String modifier;

	/** 最后更新时间 **/
	private String lastUpdatedDate;

	/** 参数值 **/
	private transient String value;

	public final static Specification<IfParameter> EmptySpecification = new Specification<IfParameter>() {
		@Override
		public Predicate toPredicate(Root<IfParameter> root,
				CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};

	public static Specification<IfParameter> idSpecification(String id) {
		return new Specification<IfParameter>() {
			@Override
			public Predicate toPredicate(Root<IfParameter> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}

	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "IF_PARAMETER_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "IF_PARA_TYPE", length = 2)
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Column(name = "PARA_DATA_TYPE", length = 2)
	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	@Column(name = "PARA_NAME", length = 50)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "IS_NEED_PARA", length = 2)
	public String getIsNeed() {
		return isNeed;
	}

	public void setIsNeed(String isNeed) {
		this.isNeed = isNeed;
	}

	@Column(name = "PARA_DEFAULT_VALUE", length = 50)
	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	@Column(name = "PARA_DESC", length = 50)
	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	@Column(name = "PARA_POS", length = 50)
	public String getPos() {
		return pos;
	}

	public void setPos(String pos) {
		this.pos = pos;
	}

	@Column(name = "IS_ENABLE", length = 2)
	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}

	@CreatedBy
	@Column(name = "CREATOR", length = 50, updatable = false)
	public String getCreator() {
		return creator;
	}

	@Column(name = "IF_NAME", length = 50)
	public void setCreator(String creator) {
		this.creator = creator;
	}

	@Column(name = "CREATED_DATE", updatable = false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "MODIFIER", length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "LAST_UPDATED_DATE", length = 50)
	public String getLastUpdatedDate() {
		return lastUpdatedDate;
	}

	public void setLastUpdatedDate(String lastUpdatedDate) {
		this.lastUpdatedDate = lastUpdatedDate;
	}

	@ManyToOne
	@JoinColumn(name = "IF_ID")
	public Http getHttp() {
		return http;
	}

	public void setHttp(Http http) {
		this.http = http;
	}

	@Transient
	public String getValue() {
		if (StringUtils.isBlank(value)) {
			value = defaultValue;
		}
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	@Transient
	public String getDataTypeName() {
		try {
			return IfParameterType.get(this.dataType).ValueName;
		} catch (Exception e) {
			return "String";
		}
	}
	
	@Transient
	public String getCreateTimeForExport(){
		if(createTime != null){
			return createTime.toString().substring(0, createTime.toString().length() - 2);
		}else{
			return "";
		}
		
	}
	
	@Transient
	public String getModifyTimeForExport(){
		if(lastUpdatedDate != null){
			return lastUpdatedDate.toString().substring(0, lastUpdatedDate.toString().length() - 2);
		}else{
			return "";
		}
	}

}